﻿package mortal.game.view.task.data
{
    import Message.DB.Tables.*;
    import Message.Game.*;
    import Message.Public.*;
    import mortal.component.gLinkText.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.game.view.guide.*;
    import mortal.mvc.core.*;

    public class TaskUtil extends Object
    {

        public function TaskUtil()
        {
            return;
        }// end function

        public static function isDramaStep(param1:TaskInfo, param2:int) : Boolean
        {
            var _loc_3:* = param1.stask.processMap[param2] as Array;
            if (_loc_3 == null)
            {
                return false;
            }
            if (_loc_3[0] == null)
            {
                return false;
            }
            var _loc_4:* = _loc_3[0] as SProcess;
            if (_loc_4.type == ETaskProcess._ETaskProcessDrama)
            {
                return true;
            }
            return false;
        }// end function

        public static function isGuildTask(param1:STask) : Boolean
        {
            if (param1 == null)
            {
                return false;
            }
            if (param1.group == ETaskGroup._ETaskGroupGuild)
            {
                return true;
            }
            return false;
        }// end function

        public static function isTaskCurNeedGuide(param1:TaskInfo, param2:GLinkTextData = null, param3:Boolean = false) : Boolean
        {
            var _loc_4:Array = null;
            if (param1 == null)
            {
                return false;
            }
            if (Cache.instance.role.roleInfo.level >= GameConst.GuideTaskWhenWaitLevlUpgradeLV)
            {
                return false;
            }
            if (param1.stask.group != ETaskGroup._ETaskGroupMain)
            {
                return false;
            }
            if (param1.isFail() || param1.isCanNotEnd())
            {
                return false;
            }
            if (param2 == null)
            {
                _loc_4 = Cache.instance.task.getLinkTextData(param1.stask.code);
                if (_loc_4 != null && _loc_4[param1.curStep] != null)
                {
                    param2 = _loc_4[param1.curStep][0] as GLinkTextData;
                }
            }
            if (param2 == null)
            {
                return false;
            }
            if (param2.type == GLinkTextData.self || param2.type == GLinkTextData.drama)
            {
                return false;
            }
            return true;
        }// end function

        public static function isFailTaskNeedComplete(param1:TaskInfo) : Boolean
        {
            return param1.stask.group == ETaskGroup._ETaskGroupEscort || param1.stask.code == GameConst.TFreeCreateGuildTaskCode;
        }// end function

        public static function isEscortTask(param1:TaskInfo) : Boolean
        {
            return param1.stask.group == ETaskGroup._ETaskGroupEscort || param1.stask.group == ETaskGroup._ETaskGroupRobEscort;
        }// end function

        public static function isLoopTask(param1:TaskInfo) : Boolean
        {
            return param1.stask.group == ETaskGroup._ETaskGroupLoop;
        }// end function

        public static function linkTask(param1:TaskInfo, param2:Boolean = false) : Boolean
        {
            var _loc_4:GLinkTextData = null;
            if (param1 == null)
            {
                return false;
            }
            var _loc_3:* = Cache.instance.task.getLinkTextData(param1.stask.code);
            if (_loc_3 != null && _loc_3.length > 0 && _loc_3[param1.curStep] != null && _loc_3[param1.curStep][0] != null)
            {
                _loc_4 = _loc_3[param1.curStep][0] as GLinkTextData;
                if (_loc_4 == null)
                {
                    return false;
                }
                if (param2)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.FlyBoot_GLinkText, _loc_4));
                    return true;
                }
                linkTaskImpl(_loc_4);
                return true;
            }
            return false;
        }// end function

        public static function linkTaskImpl(param1:GLinkTextData, param2:String = null) : void
        {
            var _loc_3:int = 0;
            var _loc_4:String = null;
            var _loc_5:int = 0;
            if (param2 == null)
            {
                _loc_3 = param1.htmlText.indexOf("event:") + 6;
                if (_loc_3 >= 6)
                {
                    _loc_4 = param1.htmlText.substr(_loc_3);
                    _loc_5 = _loc_4.indexOf("\'>");
                    param2 = _loc_4.substring(0, _loc_5);
                }
            }
            if (param1.type == GLinkTextData.branchGuide)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskClickBranchGuide, [param1, param2]));
            }
            else if (param2 == null || param2.length <= 2)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTraceTargetEvent, param1));
            }
            else if (param2 == EventName.TaskGuideShowGuildShareList)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.GuildTaskShowShareTaskListOnly));
            }
            else
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackClickEvent, [param2, param1]));
            }
            return;
        }// end function

        public static function checkAndShowFlyGuide(param1:TaskInfo) : Boolean
        {
            if (param1.stask.guide != TaskGuideType.Guide_ShowFlyGuide)
            {
                return false;
            }
            if (!canFlyByNoBuyFlyBoot())
            {
                return false;
            }
            GuideTaskFlyWin.instance.show();
            GuideTaskFlyWin.instance.updateDatas(param1);
            return true;
        }// end function

        public static function canFlyByNoBuyFlyBoot() : Boolean
        {
            var _loc_1:* = Cache.instance.pack.backPackCache.getItemCountByCategoryAndType(EGroup._EGroupProp, EProp._EPropProp, EPropType._EPropTypeConvey);
            if (Cache.instance.vip.freeFlyBootTimes == 0 && _loc_1 <= 0)
            {
                return false;
            }
            return true;
        }// end function

        public static function getLinkTargetName(param1:TaskInfo) : String
        {
            var _loc_3:String = null;
            var _loc_2:* = param1.getCurLinkData();
            switch(_loc_2.type)
            {
                case GLinkTextData.boss:
                {
                    _loc_3 = BossConfig.instance.getInfoByCode(_loc_2.value1).name;
                    break;
                }
                case GLinkTextData.npc:
                {
                    _loc_3 = NPCConfig.instance.getInfoByCode(_loc_2.value1).name;
                    break;
                }
                default:
                {
                    _loc_3 = "(" + _loc_2.x + ", " + _loc_2.y + ")";
                    break;
                    break;
                }
            }
            return _loc_3;
        }// end function

        public static function isDoingGuideTask(param1:int) : Boolean
        {
            var _loc_4:TaskInfo = null;
            var _loc_5:TTaskGuide = null;
            var _loc_2:* = Cache.instance.task.getTaskByGroupInTaskDoing(ETaskGroup._ETaskGroupMain, ETaskStatus._ETaskStatusNotComplete);
            if (_loc_2.length == 0)
            {
                return false;
            }
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                _loc_4 = _loc_2[_loc_3];
                _loc_5 = TaskConfig.instance.getTaskGuide(param1);
                if (parseInt(_loc_5.taskId) == _loc_4.stask.code)
                {
                    return true;
                }
                _loc_3++;
            }
            return false;
        }// end function

        public static function getNumByERewardType(param1:TaskInfo, param2:int) : int
        {
            var _loc_7:STaskReward = null;
            var _loc_8:SReward = null;
            if (param1 == null)
            {
                return 0;
            }
            var _loc_3:* = param1.stask.rewards;
            var _loc_4:int = 0;
            var _loc_5:* = Cache.instance.role.entityInfo.level;
            var _loc_6:int = 0;
            while (_loc_6 < _loc_3.length)
            {
                
                _loc_7 = STaskReward(_loc_3[_loc_6]);
                if (_loc_7 == null)
                {
                }
                else if (_loc_7.levelMin > _loc_5 || _loc_7.levelMax != 0 && _loc_7.levelMax < _loc_5)
                {
                }
                else if (!CareerUtil.isCareerSuit(_loc_7.career, Cache.instance.role.entityInfo.career))
                {
                }
                else
                {
                    _loc_8 = _loc_7.reward;
                    if (_loc_8.type == param2)
                    {
                        return _loc_8.num;
                    }
                }
                _loc_6++;
            }
            return 0;
        }// end function

    }
}
